<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="mainor_form">
          <div class="mainor_te">
            <el-button @click="export_de" class="mainor_tea">导出</el-button>
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="采购方式" prop="mode">
              <el-select
                v-model.trim="form.mode"
                placeholder="采购方式"
                class="wid220"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(modeList, index) in list.modeList"
                  :key="index"
                  :label="modeList"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="采购员" prop="buyer_id">
              <el-select
                v-model.trim="form.buyer_id"
                placeholder="采购员"
                class="wid220"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(buyer, index) in list.buyer"
                  :key="index"
                  :label="buyer"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="供应商" prop="supplier_id">
              <el-select
                v-model.trim="form.supplier_id"
                placeholder="供应商"
                class="wid220"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(supplier, index) in list.supplier"
                  :key="index"
                  :label="supplier"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="创建日期" prop="createTime">
              <el-date-picker
                v-model="createTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                :picker-options="pickerOptions"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="$refs['userListFrom'].resetFields()"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_num boxdq marginTop10">
          <div class="main_num_l box">
            <p>
              应付总额：
              <em>{{ list.money }}</em>
            </p>
            <p>
              已付总额：
              <em>{{ list.paid }}</em>
            </p>
            <p>
              未付总额：
              <em>{{ list.unpaid }}</em>
            </p>
          </div>
        </div>
        <div class="main_table marginTop10" style="min-height: 550px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" />
            <el-table-column label="采购方式" width="120">
              <template slot-scope="scope">{{
                list.modeList[scope.row.mode]
              }}</template>
            </el-table-column>
            <el-table-column label="采购员/供应商" width="140">
              <template slot-scope="scope">{{
                scope.row.mode == 1
                  ? list.buyer[scope.row.buyer_id]
                  : list.supplier[scope.row.supplier_id]
              }}</template>
            </el-table-column>
            <el-table-column label="应付金额" prop="money" />
            <el-table-column label="已付金额" prop="paid" />
            <el-table-column label="未付金额" prop="unpaid" />
            <el-table-column label="联系人" prop="name" />
            <el-table-column label="手机号" prop="mobile" />
            <el-table-column
              label="管理操作"
              align="center"
              fixed="right"
              width="140"
              class-name="main_hei"
            >
              <template>
                <router-link to="/financial/recharge">
                  <el-link type="success">结算</el-link>
                </router-link>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  account_generalLedger,
  export_account_info,
} from "@/api/financial/financialPre";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import dateOption from "@/options/dateOption";
import { getWeekStartAndEnd } from "@/utils/index";

export default {
  components: {},
  data() {
    return {
      // 列表数据
      list: {
        list: [],
      },
      listLoading: false,
      // 搜索
      form: {
        start_time: "",
        end_time: "",
        mode: null,
        buyer_id: null,
        supplier_id: null,
      },
      createTime: getWeekStartAndEnd(),
      pickerOptions: dateOption,
      selectArr: [],
    };
  },
  created() {
    // 列表
    this.account_generalLedger();
  },
  methods: {
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item, index) => {
        this.selectArr.push(index + 1);
      });
      console.log(this.selectArr);
    },
    // 导出明细
    export_de() {
      let ids = "";
      if (this.selectArr.length > 0) {
        ids = this.selectArr.join("-");
      }
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.list_form())) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { ids: ids },
        this.list_form()
      );
      export_account_info(data)
        .then((res) => {
          const link = document.createElement("a");
          let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          link.download = "采购总账";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
    // 搜索
    getArticleList() {
      this.account_generalLedger();
    },
    // 列表
    account_generalLedger() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      account_generalLedger(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.createTime[0] ? this.createTime[0] : "";
      form.end_time = this.createTime[1] ? this.createTime[1] : "";

      return form;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 120px);
  }
}
</style>
